package com.simen.kv;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

/**
 * Created by ABC on 2017/4/13.
 */
public class KVUtils {

    public static void runSql(String sql, String[] args, RunSqlListener runSqlListener) {
        SQLiteDatabase database = KVManager.getInstance().getLiteOpenHelper().getReadableDatabase();
        database.beginTransaction();
        try {
            Cursor cursor = database.rawQuery(sql, args);
            if (runSqlListener != null) {
                runSqlListener.onResult(cursor);
            }
            if (cursor != null) {
                cursor.close();
            }
            database.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            database.endTransaction();
            database.close();
        }
    }

    /**
     * Execute a single SQL statement that is NOT a SELECT/INSERT/UPDATE/DELETE.
     * For example, the following are good candidates for using this method:
     * <ul>
     * <li>ALTER TABLE</li>
     * <li>CREATE or DROP table / trigger / view / index / virtual table</li>
     * <li>REINDEX</li>
     * <li>RELEASE</li>
     * <li>SAVEPOINT</li>
     * <li>PRAGMA that returns no data</li>
     * </ul>
     *
     * @param sql      the SQL statement to be executed. Multiple statements separated by
     *                 semicolons are
     *                 not supported.
     * @param bindArgs only byte[], String, Long and Double are supported in bindArgs.
     * @throws SQLException if the SQL string is invalid
     */
    public static void runSql(String sql, Object[] bindArgs) {
        SQLiteDatabase database = KVManager.getInstance().getLiteOpenHelper().getWritableDatabase();
        database.beginTransaction();
        try {
            database.execSQL(sql, bindArgs);
            database.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            database.endTransaction();
            database.close();
        }
    }

    public static int update(String table, ContentValues values, String whereClause, String[]
            whereArgs) {
        SQLiteDatabase database = KVManager.getInstance().getLiteOpenHelper().getWritableDatabase();
        database.beginTransaction();
        int affectRow = 0;
        try {
            affectRow = database.update(table, values, whereClause, whereArgs);
            database.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            database.endTransaction();
            database.close();
        }
        return affectRow;
    }

    public static int delete(String table, String whereClause, String[] whereArgs) {
        SQLiteDatabase database = KVManager.getInstance().getLiteOpenHelper().getWritableDatabase();
        database.beginTransaction();
        int affectRow = 0;
        try {
            affectRow = database.delete(table, whereClause, whereArgs);
            database.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            database.endTransaction();
            database.close();
        }

        return affectRow;
    }

    /**
     * 生成where语句
     *
     * @param where 查询条件
     * @return 返回格式化的查询字符串
     */
    public static String whereToSQL(Where where) {
        String whereSql = where == null ? "" : where.toSql();
        return TextUtils.isEmpty(whereSql) ? "" : whereSql;
    }
}
